<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <customDeptTree @clickDeptNode="clickDeptNode" :show-search-input="true"/>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
          <el-form-item label="手机号" prop="account">
            <el-input v-model="queryParams.account" placeholder="手机号" clearable size="small"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="场景名称" fingerPrintName="">
            <el-input v-model="queryParams.sceneName" placeholder="场景名称" clearable size="small"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="设备" prop="deviceNameOrMac">
            <el-input v-model="queryParams.deviceNameOrMac" placeholder="设备名称或mac" clearable size="small"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
                       v-hasPermi="['system:detail:sceneBinding:query']"
            >搜索
            </el-button>
            <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                       v-hasPermi="['system:detail:sceneBinding:export']"
            >导出
            </el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="sceneBindingList" border>
          <el-table-column label="手机号" align="center" prop="account" width="120"/>
          <el-table-column label="家庭名称" align="center" prop="homeName" min-width="120" :show-overflow-tooltip="true"/>
          <el-table-column label="设备名称" align="center" prop="deviceName" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <el-popover trigger="hover" placement="bottom-start">
                <el-descriptions :column=1 style="width: 250px" :labelStyle="{'font-weight': 'bold'}">
                  <el-descriptions-item label="设备名称">{{ scope.row.deviceName }}</el-descriptions-item>
                  <el-descriptions-item label="设备名MAC">{{ scope.row.deviceMac }}</el-descriptions-item>
                  <el-descriptions-item label="设备类型">{{ scope.row.deviceType }}</el-descriptions-item>
                  <el-descriptions-item label="类型名称">{{ scope.row.categoryName }}</el-descriptions-item>
                </el-descriptions>

                <span slot="reference">
                  {{ scope.row.deviceName }}
                </span>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="开关路数" align="center" prop="wayNumber" width="80"/>
          <el-table-column label="场景名称" align="center" prop="sceneName" :show-overflow-tooltip="true"/>
          <el-table-column label="场景编号" align="center" prop="sceneCode" width="80"/>
          <el-table-column label="组织名称" align="center" prop="deptName" :show-overflow-tooltip="true"/>
          <el-table-column label="创建时间" align="center" prop="createTime" width="160"/>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getSceneBindingList"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { exportSceneBinding, listSceneBinding } from '@/api/system/sceneBinding'

export default {
  name: 'index',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      sceneBindingList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        account: undefined,
        sceneName: undefined,
        deptId: undefined,
        deviceNameOrMac: undefined
      }
    }
  },
  created() {
    this.getSceneBindingList()
  },
  methods: {
    getSceneBindingList() {
      this.loading = true
      listSceneBinding(this.queryParams).then(response => {
        this.sceneBindingList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getSceneBindingList()
    },

    /**
     * 点击树节点
     * @param value
     */
    clickDeptNode(value) {
      this.queryParams.deptId = value.id
      this.getSceneBindingList()
    },

    /** 导出按钮操作 */
    handleExport() {
      this.$confirm('是否确认导出?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return exportSceneBinding(this.queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    }
  }
}
</script>

<style scoped>

</style>
